/*
 * #depends subsonic-login.js
 */
 
Ext.ux.mattgoldspink.subsonic.FolderLoader = Ext.extend(Ext.tree.TreeLoader, {
    constructor: function(config){
        Ext.apply(config, {
            url: Ext.ux.mattgoldspink.subsonic.apiUrl + '/rest/getMusicFolders.view',
            baseParams: Ext.apply({}, Ext.ux.mattgoldspink.subsonic.LoginDetails),
            requestMethod: 'GET',
            nodeParameter: 'id',
            listeners: {
                beforeload:this.handleBeforeLoad,
                scope: this
            }
        });
        Ext.ux.mattgoldspink.subsonic.FolderLoader.superclass.constructor.apply(this, arguments);
    },
    handleBeforeLoad: function(treeloader, node, callback){
        treeloader.url = node.attributes.nextUrl;
        treeloader.root = node.attributes.nextRoot;
        if (node.attributes.nextRoot === 'indexes.index'){
            treeloader.baseParams.musicFolderId = node.id;
        }
    },
    // private override
    processResponse : function(response, node, callback, scope){
        try {
        var paths = this.root.split('.');
        for (var i = 0; i < paths.length; i++) {
            response.responseData = response.responseData[paths[i]];
        }
        if (!Ext.isArray(response.responseData)){
            response.responseData = [response.responseData];
        }
        } catch (e) {}
        Ext.ux.mattgoldspink.subsonic.FolderLoader.superclass.processResponse.apply(this, arguments);
    },
    setupNode: function(node, nextUrl, nextRoot, loaded, expanded, iconCls, text, leaf){
        return Ext.apply(node, {
            nextUrl: Ext.ux.mattgoldspink.subsonic.apiUrl + '/rest/' + nextUrl + '.view',
            nextRoot: nextRoot,
            loaded: loaded,
            expanded: expanded,
            iconCls: iconCls,
            text: text,
            leaf: (Ext.isDefined(leaf)? leaf: false)
        });
    },
    handleIndexNode: function(attr) {
        this.setupNode(attr, undefined, undefined, true, Ext.ux.mattgoldspink.subsonic.UserPrefsStore.get('auto-expand-indexes', true), 'folder', attr.name);
        if (!Ext.isArray(attr.artist)) {
            attr.artist = [attr.artist];
        }
        var index = Ext.ux.mattgoldspink.subsonic.FolderLoader.superclass.createNode.call(this, attr);
        for (var i = 0 ; i < attr.artist.length; i++) {
            index.appendChild(this.createArtistNode(attr.artist[i]));
        }
        return index;
    },
    createArtistNode: function(attr) {
        attr.title = attr.name;
        this.handleDirectory(attr);
        return Ext.ux.mattgoldspink.subsonic.FolderLoader.superclass.createNode.call(this, attr);
    },
    handleMusicFolder: function(attr){
        this.setupNode(attr, 'getIndexes', 'indexes.index', false, false, 'music', attr.name);
    },
    handlePlaylist: function(attr) {
        this.setupNode(attr, undefined, undefined, true, false, 'playlist', attr.name, true);
        attr.playlist = true;
        attr.subject = 'subsonic.folder.click';
    },
    handleDirectory: function(attr){
        this.setupNode(attr, 'getMusicDirectory', 'directory.child', false, false, 'ipod', attr.title);
        attr.subject = 'subsonic.folder.click';
    },
    handleMusicTrack: function(attr) {
        this.setupNode(attr, undefined, undefined, false, false, 'music', attr.title, true);
        attr.subject = 'subsonic.track.play';
    },
    createNode : function(attr){
        var me = this;
        if (me.root === 'indexes.index') {
            return me.handleIndexNode(attr);
        } else if (me.root === 'musicFolders.musicFolder'){
            me.handleMusicFolder(attr);
        } else if (me.root ===  'playlists.playlist') {
            me.handlePlaylist(attr);
        } else if (me.root === 'directory.child') {
            if (attr.isDir) {
                me.handleDirectory(attr);
            } else {
                me.handleMusicTrack(attr);
            }
        }
        return Ext.ux.mattgoldspink.subsonic.FolderLoader.superclass.createNode.call(me, attr);
     }
});
